package com.pocket.demo.elasticsearch.doc;

import com.pocket.demo.business.po.Hotel;
import com.pocket.demo.elasticsearch.constant.IndexNameConst;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.data.elasticsearch.annotations.GeoPointField;


/**
 * 【demo-elasticsearch-2.2】
 */
@Document(indexName = IndexNameConst.HOTEL_INDEX)
@Data
@NoArgsConstructor
public class HotelDoc {

    // 酒店id。主键一般在ES中为字符串类型，且定义为 Keyword
    @Id
    @Field(type = FieldType.Keyword)
    private String id;

    // 酒店名称；例：7天酒店
    @Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "")
    private String name;

    // 酒店地址；例：航头路
    @Field(type = FieldType.Text, index = false)
    private String address;

    // 酒店价格；例：329
    @Field(type = FieldType.Integer)
    private Integer price;

    // 酒店评分；例：45，就是4.5分
    @Field(type = FieldType.Integer)
    private Integer score;

    // 酒店品牌；例：如家
    @Field(type = FieldType.Keyword, copyTo = "keyWords")
    private String brand;

    // 所在城市；例：上海
    @Field(type = FieldType.Keyword, copyTo = "keyWords")
    private String city;

    // 酒店星级，从低到高分别是：1星到5星，1钻到5钻
    @Field(name="starName", type = FieldType.Keyword, copyTo = "keyWords")
    private String starName;

    // 商圈；例：虹桥
    @Field(type = FieldType.Keyword, copyTo = "keyWords")
    private String business;

    // 经纬度坐标 （latitude, longitude）
    @GeoPointField
    private String location;

    // 酒店图片；例:/img/1.jpg
    @Field(type = FieldType.Keyword, index = false)
    private String pic;

    // 关键字组合：酒店品牌、酒店星级、所在城市、商圈
    @Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "")
    private String keyWords;


    public HotelDoc(Hotel hotel) {
        this.id = String.valueOf(hotel.getId());
        this.name = hotel.getName();
        this.address = hotel.getAddress();
        this.price = hotel.getPrice();
        this.score = hotel.getScore();
        this.brand = hotel.getBrand();
        this.city = hotel.getCity();
        this.starName = hotel.getStarName();
        this.business = hotel.getBusiness();
        this.location = hotel.getLatitude() + ", " + hotel.getLongitude();
        this.pic = hotel.getPic();
    }
}
